//
//  TeacherViewController.m
//  ZhiBoYuan
//
//  Created by coder_di on 2018/4/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "TeacherViewController.h"
#import "AAcell.h"
#import "DCell.h"
#import "TeacherSheZhiViewController.h"
#import "TicherKeChengViewController.h"
#import "BanJiTableViewController.h"
#import "TeacherQianBaoViewController.h"
//#import "MyMessageViewController.h"
#import "SheZhiViewController.h"
#import "TiXianViewController.h"

#import "XiaoXIViewController.h"

//static NSString * ident = @"DCell";

#import "WoTableViewCell.h"

static NSString *identfire = @"WoTableViewCell";

#import "WoHeaderCell.h"
static NSString *identHead = @"WoHeaderCell";


@interface TeacherViewController ()

{
    UIButton *button;
}

@property (nonatomic,strong)NSMutableArray *selectorPatnArray;//存放选中数据

@end


@implementation TeacherViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self loadUnReadMessage];
    
    //刷新用户数据，学豆变化
    [self loadUserData];
}

-(void)loadUnReadMessage{
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{@"max_news_id":[NSString stringWithFormat:@"%@",[ZYUserManager userInfo].unreadMessageId]
                       };
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@g=Client&m=Ucenter&a=newsCount",HTTPP] parameter:parameter sucess:^(id responseObject) {
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] intValue] == 200) {
            UserinfoModel *model = [ZYUserManager userInfo];
            model.news_count = [responseObject[@"data"] valueForKey:@"news_count"];
            [ZYUserManager saveUserInfo:model];
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:responseObject[@"msg"] delay:2.0f];
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

-(void)loadUserData{
    SGParameter *parameter  = [[SGParameter alloc] init];
    
    parameter.body = @{
                       };
    
    NSString *lastUrl  = @"g=Client&m=Ucenter&a=getUserInfo";
    
    [SGHttpTool POST:[NSString stringWithFormat:@"%@%@",HTTPP,lastUrl] parameter:parameter sucess:^(id responseObject) {
        
        NSDictionary *dic = responseObject;
        NSLog(@"%@",dic);
        if ([dic[@"status"] intValue] == 200) {
          
            UserinfoModel *model = [ZYUserManager userInfo];
            model.left_xuedou = [NSString stringWithFormat:@"%@",[dic[@"data"][@"user_info"] valueForKey:@"left_xuedou"]];
            model.sex = model.sex = [NSString stringWithFormat:@"%@",[dic[@"data"][@"user_info"] valueForKey:@"sex"]];
            
            [ZYUserManager saveUserInfo:model];
            
            [self.tableView reloadData];
        }else{
            [MBProgressHUD showTextOnly:self.view message:dic[@"msg"] delay:2.0];
            
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [Helper setMoRenNav:self.navigationController];
    [Helper setNavColorFrom:self.navigationController color:ZhuTiColor];

    
        self.dataArray = @[
                           @[],
                           @[@"我的课程",@"我的钱包",@"我的班级",@"我的消息"],
                           @[@"设置"]
                           ].mutableCopy;
    
    [self.view addSubview:self.tableView];
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,15 * KscreenHeight)];
    
    view.backgroundColor = hui1Color;
    
    return view;
    
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return 0;
    }
    return 15 * KscreenHeight;
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1){
        
        NSArray *arr =self.dataArray[section];
        return arr.count;
    }else{
        return 1;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        WoHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:identHead forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //充值
        [cell.chonZhiButton setTitle:@"提现" forState:0];
        [cell.chonZhiButton addTarget:self action:@selector(tixianAction) forControlEvents:(UIControlEventTouchUpInside)];
        
        NSString *headUrl = [ZYUserManager userInfo].small_img;
        [cell.imageHead sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",headUrl]] placeholderImage:[UIImage imageNamed:@"图层18"]];
        
        cell.nmaLlabel.text = [ZYUserManager userInfo].nick_name;
        
        cell.labelB.text = [NSString stringWithFormat:@"%@",[ZYUserManager userInfo].left_xuedou];
        
        return cell;
    }else  if (indexPath.section == 1){
        WoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfire forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        
        if (indexPath.row ==5 ) {
            
            if ([[ZYUserManager  userInfo].news_count isEqualToString:@"0"]) {
                cell.view.backgroundColor = [UIColor clearColor];
            }else{
                cell.view.backgroundColor = [UIColor redColor];
                cell.view.text = [ZYUserManager  userInfo].news_count;
            }
            
        }else{
            cell.view.hidden = YES;
            
        }
        cell.label.text = self.dataArray[indexPath.section][indexPath.row];
        
        
        return cell;
    }else{
        WoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfire forIndexPath:indexPath];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        cell.view.hidden = YES;
        
        cell.label.text = self.dataArray[indexPath.section][indexPath.row];
        
        
        return cell;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        return 100;
    }else{
        return 60;
    }
    
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        TeacherSheZhiViewController *gerenVC = [[TeacherSheZhiViewController alloc]init];
        gerenVC.hidesBottomBarWhenPushed = YES;
        
        [self.navigationController pushViewController:gerenVC animated:YES];
        
    }else if (indexPath.section == 1) {
        
        if (indexPath.row == 0) {
            TicherKeChengViewController *keCHengVC = [[TicherKeChengViewController alloc]init];
            keCHengVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:keCHengVC animated:YES];
        }else if (indexPath.row == 1){
            TeacherQianBaoViewController *QianBaoVC = [[TeacherQianBaoViewController alloc]init];
            QianBaoVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:QianBaoVC animated:YES];
            
        }else if (indexPath.row == 2){
            BanJiTableViewController *banJiVC = [[BanJiTableViewController alloc]init];
            banJiVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:banJiVC animated:YES];
            
        }else if (indexPath.row == 3){
//            MyMessageViewController *myMessageVC = [[MyMessageViewController alloc]init];
//            myMessageVC.hidesBottomBarWhenPushed = YES;
//
//            [self.navigationController pushViewController:myMessageVC animated:YES];
            
            XiaoXIViewController *messageVC = [[XiaoXIViewController alloc]init];
            messageVC.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:messageVC animated:YES];
            
        }
        
    }else if (indexPath.section == 2 ){
        if (indexPath.row == 0) {
            //设置
            SheZhiViewController *shezhiVC = [[SheZhiViewController alloc]init];
            shezhiVC.hidesBottomBarWhenPushed = YES;
            
            [self.navigationController pushViewController:shezhiVC animated:YES];
        }
    }
    
        
}



#pragma makr -- property
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *  tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, Screenheight- NAVIGATION_BAR_HEIGHT -TAB_BAR_HEIGHT) style:UITableViewStylePlain];
        tableView.backgroundColor = [UIColor clearColor];
        tableView.delegate        = self;
        tableView.dataSource      = self;
        tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        
        tableView.estimatedRowHeight = 44.0f * KscreenHeight;//推测高度，必须有，可以随便写多少
        
        tableView.rowHeight = UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
        
//        tableView.scrollEnabled = NO;
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        
        //        self.extendedLayoutIncludesOpaqueBars = YES;
        
        if (@available(iOS 11.0, *)) {
            tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        tableView.contentInset = UIEdgeInsetsMake(0, 0, 49 * KscreenHeight, 0);
        tableView.scrollIndicatorInsets = _tableView.contentInset;
        
        [tableView registerClass:[WoTableViewCell class] forCellReuseIdentifier:identfire];
        [tableView registerClass:[WoHeaderCell class] forCellReuseIdentifier:identHead];
        
        
        [tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
        _tableView = tableView;
        
    }
    return _tableView;
}


//提现事件
-(void)tixianAction{
    TiXianViewController *tixianVC = [[TiXianViewController alloc]init];
    tixianVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:tixianVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
@end
